using System;
using System.Runtime.CompilerServices;
using ThemeChain;

// ReSharper disable UnusedMember.Local

namespace Yoshi.Classes
{
    [IgnoreNamespace]
    public class UriParse
    {
        [AlternateSignature]
        private static extern string Unanchor(string m);

        // ReSharper disable UnusedParameter.Local
        private static string Unanchor(string match, string m1, string m2)
        // ReSharper restore UnusedParameter.Local
        {
            string ot = m2.ReplaceRegex((RegularExpression)Script.Literal(@"/([\s\S]*?=[\s\S]*?$)|([^=]*$)/"), (StringReplaceCallback)Script.Literal(@"function(x,x1,x2){return x1?'""'+jQuery.trim(x1)+'""':jQuery.trim(x2);}"));
            m1 = UnClassAction(m1) ?? ThemeYoshi.UnSmartUrl(m1);
            return "(( " + ot + (!String.IsNullOrEmpty(m1) ? " = " + m1 : "") + " ))";
        }

        public static string ParseAnchorsToEdit(string ot)
        {
            ot = ot.ReplaceRegex((RegularExpression)Script.Literal(@"/<a\s+.*?href=""([^""]*)""[^>]*?>(.*?)<\/a>/gi"), Unanchor);
            return ot;
        }

        [AlternateSignature]
        private static extern string Anchor(string match);

        // ReSharper disable UnusedParameter.Local
        private static string Anchor(string match, string nm, string uri)
        // ReSharper restore UnusedParameter.Local
        {
            nm = nm.Trim().ReplaceRegex((RegularExpression)Script.Literal(@"/^[""']|[""']$/g"), "");
            string ca = ClassAction(uri);
            if (ca != null)
                return String.Format(@"<a class=""UserLink"" href=""javascript:Master.DoMethod('{1}');"">{0}</a>", nm, ca);
            string u = ThemeYoshi.SmartUrl(uri);
            if (ThemeYoshi.IsCurateUrl(u))
                return (string)Script.Literal(@"'<a class=""UserLink"" href=""'+({1})+'"">'+{0}+'</a>'", nm, u);
            return (string)Script.Literal(@"'<a class=""UserLink"" href=""'+({1})+'"" target=""_blank"">'+{0}+'</a>'", nm, u);
        }

        private static readonly RegularExpression ClassActionRx = (RegularExpression) Script.Literal(@"/method:\s*([a-z\d.]+)/i");
        private static string ClassAction(string uri)
        {
            ClassActionRx.LastIndex = 0;
            string[] mx = ClassActionRx.Exec(uri);
            return mx != null ? mx[1].Trim() : null;
        }

        private static readonly RegularExpression UnClassActionRx = (RegularExpression)Script.Literal(@"/javascript:\s*Master.DoMethod\(['""](\w+\.[\w\d]+)['""]\)/");
        private static string UnClassAction(string uri)
        {
            UnClassActionRx.LastIndex = 0;
            string[] mx = UnClassActionRx.Exec(uri);
            return mx != null ? String.Format("method:{0}", mx[1].Trim()) : null;
        }

        public static string ParseEditToAnchors(string ot)
        {
            ot = ot.ReplaceRegex((RegularExpression)Script.Literal(@"/\(\(\s*?(""[^""\\]*(?:\\[\s\S][^""=\\]*)*""|'[^'\\]*(?:\\[\s\S][^'\\]*)*'|[^=""']+?)\s*(?:=\s*([\s\S]*?)\s*\)\)|\s*\)\))/gm"), Anchor);
            return ot;
        }
    }
}
